#define TRACE
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace ListaFacil
{
    public partial class frmAssociarSegmento : Form
    {
        private int ddd, numero;
        private string titular;
        private bool editMode = false;
        private DataTable dtTelefoneSegmento, dtSegmento;
        private BindingSource bsTelefoneSegmento, bsSegmento;

        public frmAssociarSegmento(int ddd, int numero, string titular)
        {
            InitializeComponent();
            this.bsSegmento = new BindingSource();
            this.bsTelefoneSegmento = new BindingSource();

            this.ddd = ddd;
            this.numero = numero;
            this.titular = titular;
            
        }
        private void frmAssociarSegmento_Load(object sender, EventArgs e)
        {
            this.consultarTelefoneSegmento();
            this.consultarSegmentos();
            this.cboSegmento.SelectedIndex = -1;
            this.ckBoxAnunciante.Checked = false;
            this.txtTitular.Text = this.titular.ToString();
            this.HabilitaBotoes(true);
        }

        private void consultarTelefoneSegmento()
        {
            this.dtTelefoneSegmento = Program.SLF.consultarTelefoneSegmento(this.ddd, this.numero);
            this.bsTelefoneSegmento.DataSource=this.dtTelefoneSegmento;
            this.bindingNavigator1.BindingSource=this.bsTelefoneSegmento;
            this.dgvSegmentoTelefone.DataSource=this.bsTelefoneSegmento;
        }

        private void consultarSegmentos()
        {
            this.dtSegmento=Program.SLF.consultarSegmentoCadastrado();
            this.bsSegmento.DataSource=this.dtSegmento;
            this.cboSegmento.DataSource = bsSegmento.DataSource;
            this.cboSegmento.DisplayMember = "Nome";
            this.cboSegmento.ValueMember = "ID_Segmento";
        }

        private void dgvSegmentoTelefone_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvSegmentoTelefone.CurrentRow != null)
            {
                txtTitular.Text = dgvSegmentoTelefone.CurrentRow.Cells["nome"].Value.ToString();
                cboSegmento.SelectedText = dgvSegmentoTelefone.CurrentRow.Cells["segmento"].Value.ToString();
                if (Convert.ToBoolean(dgvSegmentoTelefone.CurrentRow.Cells["anunciante"]) == true)
                    ckBoxAnunciante.Checked = true;
                else
                    ckBoxAnunciante.Checked = false;
            }
        }

        private void HabilitaBotoes(bool flagHabilita)
        {
            addItem.Enabled = flagHabilita;
            deleteItem.Enabled = flagHabilita;
            bindingNavigatorMoveFirstItem.Enabled = flagHabilita;
            bindingNavigatorMoveLastItem.Enabled = flagHabilita;
            bindingNavigatorMoveNextItem.Enabled = flagHabilita;
            bindingNavigatorMovePreviousItem.Enabled = flagHabilita;
            bindingNavigatorPositionItem.Enabled = flagHabilita;
            saveItem.Enabled = !flagHabilita;
            cancelItem.Enabled = !flagHabilita;
            dgvSegmentoTelefone.Enabled= flagHabilita;
            cboSegmento.Enabled = !flagHabilita;
            txtTitular.Enabled = !flagHabilita;
            ckBoxAnunciante.Enabled = !flagHabilita;
            this.cboSegmento.SelectedIndex = -1;
        }

        private void editToolStripButton_Click(object sender, EventArgs e)
        {
            this.HabilitaBotoes(false);
            this.editMode = true;
        }

        private void cancelToolStripButton_Click(object sender, EventArgs e)
        {
            this.HabilitaBotoes(true);
            this.consultarTelefoneSegmento();
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            
            string retorno = "";
            if (cboSegmento.SelectedIndex > -1)
            {
                retorno = Program.SLF.associarSegmento(this.ddd, this.numero,Convert.ToInt32( cboSegmento.SelectedValue), ckBoxAnunciante.Checked, txtTitular.Text);
                this.HabilitaBotoes(true);
                this.consultarTelefoneSegmento();
            }
            else
                MessageBox.Show("Favor selecionar um segmento");
            
        }        


        private void bindingNavigatorPositionItem_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsDigit(e.KeyChar) == false)
                e.Handled = true;
        }

        private void Navegacao(object sender, EventArgs e)
        {
            this.editMode = false;
        }

        private void edicao(object sender, EventArgs e)
        {
            this.editMode = true;
        }

        private void bindingNavigatorDeleteItem_Click_1(object sender, EventArgs e)
        {
            int numeroregistro = 0;
            int linha = 0;
            string retorno = "";

            linha = Convert.ToInt32(bindingNavigatorPositionItem.Text) - 1;
            numeroregistro = Convert.ToInt32(dgvSegmentoTelefone.Rows[linha].Cells["ID"].Value);

            retorno = Program.SLF.desassociarTelefoneSegmento(numeroregistro);

            if (retorno != "")
                MessageBox.Show(retorno, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);

            this.HabilitaBotoes(true);
            this.consultarTelefoneSegmento();
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            this.HabilitaBotoes(false);
        }

        private void btnAssociar_Click(object sender, EventArgs e)
        {

        }

    }
}